<template>
  <div>
    <!-- 面包屑部分 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索栏和添加用户区域 -->
      <el-row :gutter="20">
        <el-col :span="9">
          <el-input placeholder="请输入内容" clearable v-model="queryInfo.query" @clear='getGoodList'>
            <el-button slot="append" icon="el-icon-search" @click="getGoodList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表区域 -->
      <el-table :data="goodList" style="width: 100%" border stripe>
          <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="700"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="80"></el-table-column>
        <el-table-column prop="goods_number" label="商品数量" width="80"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80"></el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editGoods(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete"  size="mini" @click="deleteGoods(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页部分区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[15, 30, 50, 80]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 添加商品对话框 -->
    <EditGoods ref="editGoods"></EditGoods>
  </div>
</template>
<script>
import EditGoods from '../good/dialog/EditGoods'
export default {
  components: {
    EditGoods
  },
  data: function () {
    return {
      goodList: [],
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 15
      },
      // 商品总条目
      total: 1
    }
  },
  created: function () {
    this.getGoodList()
  },
  methods: {
    // 获取商品列表数据
    getGoodList: async function () {
      const res = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.warning('商品列表数据获取失败')
      }
      this.goodList = res.data.goods
      this.total = res.data.total
    },
    // 监听pagesize 改变的事件
    handleSizeChange: function (pagesize) {
      this.queryInfo.pagesize = pagesize
      this.getGoodList()
    },
    // 监听当前pagenum改变的事件
    handleCurrentChange: function (pagenum) {
      this.queryInfo.pagenum = pagenum
      this.getGoodList()
    },
    // 点击按钮，删除删除商品
    deleteGoods: async function (row) {
      const confirmResult = await this.$confirm('确认删除?', '提示', {
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.warning('已取消删除')
      }
      const res = await this.$http.delete(`goods/${row.goods_id}`)
      if (res.meta.status !== 200) {
        return this.$message.warning('删除失败')
      }
      this.getGoodList()
      this.$message.success('删除成功')
    },
    // 添加商品，弹出对话框
    addGoods: function () {
      this.$router.push('/add')
    },
    editGoods: async function (row) {
      const res = await this.$http.get(`goods/${row.goods_id}`)
      if (res.meta.status !== 200) {
        return this.$message.warning('商品id查询失败')
      }
      this.$refs.editGoods.goods_id = res.data.goods_id
      this.$refs.editGoods.dialogVisible = true
    }
  }
}
</script>
<style lang="less" scoped>
.el-table {
    margin: 20px 0;
}
</style>
